﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using Models.Wishire.entity;


/*
  插入一个plan_st
 public void planInsert(plan_st plan_St)
//根据ID删除
        public void planDelete(int ID)
//根据ID更新数据
        public void planUpdate(plan_st plan_St, int ID)
//根据ID查询 只查得到一个数据
        public plan_st planFind(int ID)
 //根据用户ID查询 返回一个数组
        public List<plan_st> planFind_accoundId(string ID)
//根据城市名称查询 返回一个数组
        public List<plan_st> planFind_cityName(string Name)
 */
namespace Models.Wishire.Operator
{
    class opdataplan
    {
        //插入一个plan_st
        public void planInsert(plan_st plan_St)
        {
            string planId = plan_St.GetPlanId().ToString();
            string planName = plan_St.GetPlanName();
            string accountId = plan_St.GetAccountId().ToString();
            string cityName = plan_St.GetCityName();
            string startTime = plan_St.GetStartTime();
            string endTime = plan_St.GetEndTime();
            string ifDeleted = plan_St.GetIfDeleted().ToString();
            string setTime = plan_St.GetSetTime().ToString();
            string empty = plan_St.GetEmpty();
            string hotelName = plan_St.GethotelName().ToString();
            double hotelLat = plan_St.GethotelLat();
            double hotelLng = plan_St.GethotelLng();
            if (planId == "") planId = string.Empty;
            //string temp = "insert into plan_st (planId,planName,accountId,cityName,startTime,endTime,ifDeleted,setTime,empty,hotelName,hotelLat,hotelLng) ";
            string temp = "insert into plan_st ";
            string value = $"value({planId},'{planName}','{accountId}','{cityName}','{startTime}','{endTime}','{ifDeleted}','{setTime}',";
            value += $"'{hotelName}','{hotelLat}','{hotelLng}','{empty}')";
            string query = temp + value;
            try
            {
                MysqlOp mysqlOp = new MysqlOp();
                mysqlOp.Mysqlop(query);
                Console.WriteLine("增加成功");

            }
            catch (Exception ex)
            {
                Console.WriteLine("增加失败");
            }
        }
        //根据ID删除
        public void planDelete(int ID)
        {
            //string temp = "insert into plan_st (planId,planName,accountId,cityName,startTime,endTime,ifDeleted,setTime,empty,hotelName,hotelLat,hotelLng) ";
            string temp = "delete from  plan_st  where ";
            string value = $"planId={ID}";
            string query = temp + value;
            try
            {
                MysqlOp mysqlOp = new MysqlOp();
                mysqlOp.Mysqlop(query);
                Console.WriteLine("删除成功");
            }
            catch (Exception ex)
            {
                Console.WriteLine("删除失败");
            }
        }
        //根据ID更新数据
        public void planUpdate(plan_st plan_St)
        {
            try
            {
                int ID = plan_St.GetPlanId();
                plan_st tempans = find_planby_planId(ID);
                tempans = plan_St;
                planDelete(ID);
                planInsert(plan_St);
                Console.WriteLine("修改成功");
            }
            catch (Exception ex)
            {
                Console.WriteLine("修改失败");
            }

        }
        //根据ID查询 只查得到一个数据
        public plan_st find_planby_planId(int ID)
        {
            MysqlOp mysqlOp = new MysqlOp();
            var temp = $"select * from  plan_st  where planId={ID} ";
            var ans = mysqlOp.Mysql_planst_Query(temp);
            plan_st plan = null;
            if (ans.Count > 0) plan = ans[0];
            return plan;
        }
        //根据用户ID查询 返回一个数组
        public List<plan_st> planFind_accoundId(int ID)
        {
            MysqlOp mysqlOp = new MysqlOp();
            var temp = $"select * from  plan_st  where accountId={ID} ";
            var ans = mysqlOp.Mysql_planst_Query(temp);
            return ans;
        }
        //根据城市名称查询 返回一个数组
        public List<plan_st> planFind_cityName(string Name)
        {
            MysqlOp mysqlOp = new MysqlOp();
            var temp = $"select * from  plan_st  where cityName='{Name}'";
            var ans = mysqlOp.Mysql_planst_Query(temp);
            return ans;
        }
        public List<plan_st> planFind_Id_City(int ID,string Name)
        {
            MysqlOp mysqlOp = new MysqlOp();
            var temp = $"select * from  plan_st  where cityName='{Name}' and accountId = '{ID}'";
            var ans = mysqlOp.Mysql_planst_Query(temp);
            return ans;
        }
        public plan_st planFind_Id(int ID, string Name)
        {
            MysqlOp mysqlOp = new MysqlOp();
            var temp = $"select * from  plan_st  where planName='{Name}' and accountId = '{ID}'";
            var ans = mysqlOp.Mysql_planst_Query(temp);
            return ans[0];
        }
    }
}
